import matplotlib.pyplot as plt
import keras
print keras.__version__
from keras import regularizers
from keras import models
from keras import layers
from keras.datasets import imdb
import numpy as np



(train_data, train_labels), (test_data, test_labels) = imdb.load_data(num_words=10000)

def vectorize_sequences(sequences, dimension=10000):
    # Create an all-zero matrix of shape (len(sequences), dimension)
    results = np.zeros((len(sequences), dimension))
    for i, sequence in enumerate(sequences):
        results[i, sequence] = 1.  # set specific indices of results[i] to 1s
    return results

#---------------------下面是训练集和验证集---------------------------------
# Our vectorized training data
x_train = vectorize_sequences(train_data)
# Our vectorized test data
x_test = vectorize_sequences(test_data)
# Our vectorized labels
y_train = np.asarray(train_labels).astype('float32')
y_test =  np.asarray(test_labels).astype('float32')



#----------------以下是参考组---------------------------------------
original_model = models.Sequential()
original_model.add(layers.Dense(16, activation='relu', input_shape=(10000,)))
original_model.add(layers.Dense(16, activation='relu'))
original_model.add(layers.Dense(1, activation='sigmoid'))

original_model.compile(optimizer='rmsprop',
                       loss='binary_crossentropy',
                       metrics=['acc'])


original_hist = original_model.fit(x_train, y_train,
                                   epochs=20,#①
                                   batch_size=512,
                                   validation_data=(x_test, y_test))
#----------------以上是参考组---------------------------------------
#----------------下面是对照组---------------------------------------
bigger_model = models.Sequential()
bigger_model.add(layers.Dense(512, activation='relu', input_shape=(10000,)))#因为输入的是10000个高频词
bigger_model.add(layers.Dense(512, activation='relu'))
bigger_model.add(layers.Dense(1, activation='sigmoid'))

bigger_model.compile(optimizer='rmsprop',
                     loss='binary_crossentropy',
                     metrics=['acc'])




bigger_model_hist = bigger_model.fit(x_train, y_train,
                                     epochs=20,#②
                                     batch_size=512,
                                     validation_data=(x_test, y_test))


# Here's how the bigger network fares compared to the reference one. 
# The dots are the validation loss values of the bigger network, and the 
# crosses are the initial network.



epochs = range(1, 21)#这个范围是[1,21)  #③
original_val_loss = original_hist.history['val_loss']
bigger_model_val_loss = bigger_model_hist.history['val_loss']


plt.plot(epochs, original_val_loss, 'b+', label='Original model')
plt.plot(epochs, bigger_model_val_loss, 'bo', label='Bigger model')
plt.xlabel('Epochs')
plt.ylabel('Validation loss')
plt.legend()

plt.show()


# 
# The bigger network starts overfitting almost right away, after just one epoch, and overfits much more severely. Its validation loss is also 
# more noisy.
# 
# Meanwhile, here are the training losses for our two networks:




original_train_loss = original_hist.history['loss']

bigger_model_train_loss = bigger_model_hist.history['loss']

plt.plot(epochs, original_train_loss, 'b+', label='Original model')
plt.plot(epochs, bigger_model_train_loss, 'bo', label='Bigger model')
plt.xlabel('Epochs')
plt.ylabel('Training loss')
plt.legend()

plt.show()

#更大的神经网络也会更快地过拟合


